package com.osfw.dbadminer.service.jdbc;

import com.osfw.dbadminer.entity.connect.DbaConnect;
import com.osfw.dbadminer.enums.ConnectType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * JDBC 实例工厂
 */
@Slf4j
@Component
public class JdbcBaseFactory {


    /**
     * 创建实例对象
     *
     * @param type 数据库类型
     * @author fw on 2023
     */
    public static JdbcBaseService create(String type) {
        try {
            // 构建实例
            JdbcBaseService service = (JdbcBaseService) ConnectType.getClzByType(type).newInstance();
            return service;
        } catch (Exception e) {
            log.error(type + " 初始化实例，失败！", e);
        }
        return null;
    }

    /**
     * 创建实例对象
     *
     * @param connect 连接信息
     * @author fw on 2023
     */
    public static JdbcBaseService createAndInit(DbaConnect connect) {
        try {
            // 构建实例
            JdbcBaseService service =  (JdbcBaseService) ConnectType.getClzByType(connect.getType()).newInstance();
            service.init(connect);
            return service;
        } catch (Exception e) {
            log.error(connect.getType() + " 初始化实例，失败！" + e.getMessage());
        }
        return null;
    }

}
